home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 108_01 / datedemo.doc < prev    next >
Text File  |  1985-11-13  |  12KB  |  398 lines

  1. .OP
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.                     SOME_USEFUL_C_TIME_FUNCTIONS
  12.  
  13.  
  14.                              By Bill Bolton
  15.  
  16.                              Software Tools,
  17.  
  18.                              P.O. Box 80,
  19.  
  20.                              Newport Beach,
  21.  
  22.                              NSW, 2106
  23.  
  24.                              AUSTRALIA
  25.  
  26.  
  27.  
  28.                              Phone (02) 997-1018
  29.  
  30.  
  31. .HESOME USEFUL C TIME FUNCTIONS, By Bill Bolton              PAGE # 
  32.  
  33. .PA      
  34.                     SOME_USEFUL_C_TIME_FUNCTIONS
  35.  
  36. ABSTRACT
  37.  
  38.      Rather  than show why you SHOULD use a programming  language 
  39.  
  40. (in this case C) this article endeavours to show why I HAVE  used 
  41.  
  42. it.  An example program demonstrates some aspects of the language 
  43.  
  44. and is directly useful to owners of the Mountain Hardware 100,000 
  45.  
  46. day  clock board as well as being generally useful to others with 
  47.  
  48. real time clock facilities in their systems. A basic knowledge of 
  49.  
  50. C  will  be required to understand the  example  program,  it  is 
  51.  
  52. structured   ,commented  and  brief  extra  descriptions  of  the 
  53.  
  54. functions are given. 
  55.   
  56. WHY_C?
  57.  
  58.      While  I  have admired the C language from afar for  a  long 
  59.  
  60. time  it is only relatively recently that I have  discovered  its 
  61.  
  62. true delights. Because PASCAL was supposed to be THE language for 
  63.  
  64. structured programming and there was a need for more structure in 
  65.  
  66. my  programming  efforts,  I  had given PASCAL a  good  try.  The  
  67.  
  68. system utilities which I was interested in writing just would not 
  69.  
  70. come together properly under PASCAL,  I had the distinct  feeling 
  71.  
  72. that  the  language  was  fighting  me  rather  than  helping  my 
  73.  
  74. programming efforts.  My problems with PASCAL were not limited to 
  75.  
  76. one  implementation,  PASCAL/M,  PASCAL  MT+,  PASCAL/Z and  UCSD 
  77.  
  78. PASCAL all presented similar generic problems.  Before the PASCAL 
  79.  
  80. phreaks  rise  up again,  let me stress that there are a  lot  of 
  81.  
  82. things  about PASCAL which seem very worthwhile,  but I  couldn't 
  83.  
  84. get  I to do what the things that I need done,  in a way I  could 
  85.  
  86. grasp.
  87.  
  88.      So  for a time I retreated to assembler and FORTRAN for  the 
  89.  
  90. serious  stuff  and BASIC for the quickies.  I found that it  was 
  91.  
  92. possible to write reasonably structured programs in BASIC-80  (1) 
  93.  
  94. and  FORTRAN  80  if  a moderate amount of care  was  taken   but 
  95.  
  96. assembler was still the mainstay.  A brief flirtation with  FORTH 
  97.  
  98. followed.  FORTH  showed  great  promise but  the  temptation  to 
  99.  
  100. produce  code  which even I couldn't understand a week later  was 
  101.  
  102. too  great.   Still,  the  idea  of  an  extensible  language  is 
  103.  
  104. fascinating !
  105.  
  106.       In  the  meantime  I had been reading  "The  C  Programming 
  107.  
  108. Language"(2)  and  it was clear that C was a  language  that  was 
  109.  
  110. structured  but  not to verbose or inflexible (my major  problems 
  111.  
  112. with  PASCAL).  It also appeared to have an extensible aspect  in 
  113.  
  114. that the overhead in writing,  maintaining and using a library of 
  115.  
  116. functions was quite small.
  117.   
  118.      At  about this time a major software project of the AWA Data 
  119.  
  120. Group was started in C.  I was able to look over the programmer's 
  121.  
  122. shoulder as the project progressed and I liked what I saw. What's 
  123.  
  124. more  when  the project finished the programmers  involved  still 
  125.  
  126. seemed  sane.  I  had  a  chance to  see  a  working  C  compiler 
  127.  
  128. (Whitesmith's  for  the  PDP-11)  in  action  and  to  study  the 
  129.  
  130. documentation.  While  the  Whitesmith's compiler was out  of  my 
  131.  
  132. financial range for starters,  Leor Zolman had his BDS C compiler 
  133.  
  134. available  at a very reasonable price (on reflection I'd say  its 
  135.  
  136. an absolute steal) so that was my choice.
  137.  
  138.      As   soon  as  the compiler arrived I was  thrown  into  the 
  139.  
  140. situation  that forced me to jump in at the deep end and  use  it 
  141.  
  142. for a commercial project.  I had quoted a client 4 hours to write 
  143.  
  144. a  file conversion program in assembler,  it soon became  obvious 
  145.  
  146. that  I  had  made  a major error with my time  prediction  so  I 
  147.  
  148. decided  to try and salvage the situation by writing the  program 
  149.  
  150. in  C.  This first programming effort in C went so smoothly  that 
  151.  
  152. the project was finished within the budgeted time and the  client 
  153.  
  154. got  a  product with error handling an order of magnitude  better 
  155.  
  156. than he would have received from an assembler program. Since then 
  157.  
  158. I  have  abandoned BASIC almost entirely,  only use  FORTRAN  for 
  159.  
  160. maintenance  and  have greatly reduced the  amount  of  assembler 
  161.  
  162. programming.  The description of C as "a PASCAL that's not afraid 
  163.  
  164. to get its hands dirty"(3) is very, very apt.
  165.  
  166.      If  there  is one single thing that delayed my entry in  the 
  167.  
  168. world of C it is probably the sparsity of references to C in  the 
  169.  
  170. microcomputer  journals.  There  have  been only  a   handful  of 
  171.  
  172. programs  published and often they are examples that don't really 
  173.  
  174. do anything useful (Small C in an obvious  exception).  Following 
  175.  
  176. is one small effort to correct this situation.
  177.  
  178. THE_PROGRAM
  179.  
  180.      DATEDEMO  is a collection of C functions which will format a 
  181.  
  182. string  with  the current date and current time  fetched  from  a 
  183.  
  184. Mountain Hardware 100,000 day clock S-100 board. Several date and 
  185.  
  186. time  formats  are  available  by specification  as  one  of  the 
  187.  
  188. arguments to the primary functions.
  189.  
  190.      The  date formats are those used by the nearly all "Western" 
  191.  
  192. countries outside North America (this is an appropriate point  to 
  193.  
  194. complain  loudly about software packages that lock the user  into 
  195.  
  196. MM/DD/YY date formats !!!) but are coded in a very general manner 
  197.  
  198. so  that  nearly  any  imaginable date  format  could  be  easily 
  199.  
  200. generated  and extra formats are easily added.  Time formats  are 
  201.  
  202. somewhat  arbritary  and  only support 24 hour types  (perhaps  if 
  203.  
  204. someone is keen they will do an AM/PM 12 hour type and contribute 
  205.  
  206. it to DDJ).
  207.  
  208.       In practice I use the functions to put date and time on  in 
  209.  
  210. the  page  headings  of  hardcopy output  and  in  some  business 
  211.  
  212. packages.  The  functions live in my standard C function  library 
  213.  
  214. and are linked in at compile time.  Normally the "#define" state
  215.  
  216. ments are loaded from a header file CLOCK.H but the are listed in 
  217.  
  218. full here.  While the program is written for compilation with the 
  219.  
  220. BDS  C compiler (Version 1.4X),  it should also compile with  the 
  221.  
  222. other  C  compilers.  The only non-standard library  function  is 
  223.  
  224. 'initw' which is the BDS kludge to allow array initialisation, as 
  225.  
  226. BDS  C does not support initialisers.  As far as I know all other 
  227.  
  228. library  functions are supported by other C compilers (maybe  not 
  229.  
  230. Small C). 
  231.  
  232. THE_FUNCTIONS
  233.  
  234. 'main' -      is  simply  an executable demonstration program  to 
  235.  
  236.               tie the actual working functions together,  all the 
  237.  
  238.               presently  supported  formats are displayed  and  a 
  239.  
  240.               typical  use of the formatted strings is  shown  on 
  241.  
  242.               the last line displayed.
  243.  
  244. 'date' -      Attempts  to get basic data to format a date string 
  245.  
  246.               form the clock,  if this fails an error message  is 
  247.  
  248.               displayed.   Else  the  month  and  week  name  are 
  249.  
  250.               determined and the string formatted.
  251.  
  252. 'get_date' -  tests  if  the clock board is in the  system.  Then 
  253.  
  254.               collects digits from the clock to get the number of 
  255.  
  256.               days  since  31/Dec/1977  (N.B.   this  day  was  a 
  257.  
  258.               Saturday,  so a simple MOD 7 on the total number of 
  259.  
  260.               days since then makes Sunday day 1).  This value is 
  261.  
  262.               then  turned  into the year, month and day  of  the 
  263.  
  264.               month.
  265.  
  266. 'month_day'-  Uses  an  initialised array to determine month  and 
  267.  
  268.               day of the month from year and day of the year.
  269.  
  270. 'ndays' -     Returns number of days in the specified year.
  271.  
  272. 'leap' -      Thorough test for leap years.
  273.  
  274. 'name_month'- Fills  a string with a month name g